package com.whty.cms.filewps.util;

import org.apache.commons.codec.binary.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;

public final class CryptoServer {

    // 秘钥算法: RSA非对称加密算法
    private static final String KEY_ALGORITHM = "RSA";

    // BASE64编码
    private static Base64 base64 = new Base64();

    // 签名算法
    private static final String SIGN_ALGORITHM = "SHA1withRSA";

    // 私钥对象
    private PrivateKey privateKey;

    private static CryptoServer instance = null;

    private CryptoServer(String appSecret) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        KeySpec keySpec = new PKCS8EncodedKeySpec(base64.decode(appSecret));
        privateKey = keyFactory.generatePrivate(keySpec);
    }

    public static CryptoServer getInstance(String appSecret) {
        try {
            if (instance != null) {
                return instance;
            }
            instance = new CryptoServer(appSecret);
            return instance;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * @return
     * @author xiongxiaofei
     * @desc 私钥生成数字签名
     * @param    key
     */
    public String signature(String key) {
        try {
            Signature signature = Signature.getInstance(SIGN_ALGORITHM);
            signature.initSign(privateKey);
            signature.update(key.getBytes());
            byte[] data = signature.sign();
            return base64.encodeToString(data);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public static void main(String[] args) throws Exception {
    	String key = "16499bd775d14c9c90da2ae4c718718c&POST&/upload/getSimpleCosAuthorization&1559033723083";
    	String appSecret = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKYgK3NfVFksE4N1UeR/0RSlwjcUkrhdnzlwxGthNDaa2qm+iBHC5AEGI+ok3II7l0Qyvp+BNDn+j5w+Yc4lzWM60SzcpI5byH/vde4n0u9EdLq6hT8ANSNu5EKaoIk+ogcmde6XqMHNX0CBbfoWF6ErM6BZyoB+4glGHsSeiDQJAgMBAAECgYAFghmOuMF2N3TqLfCqL+bB1YEsDF2VCV2nLOsGWHG/wcCwUiX22MxwkECOmexJ0SoB0+DoLwOJTLx8RME/cZduviJL/bR24tpPACnwivxzGD+vDXe+1ZPOnuK9v6D+nl+A3VPftk/SrEiUfTlCWrRmx+G0gfs2xtlD57yd4tvvkQJBAOA1+Twf249irQZaMulJeREcvzh9ev86L2jQCdRazPc3ocdO4ITA98ClfJKgALCqwqLFH4r0fyl+XQ3frDk1yNUCQQC9ressikidXmtM5mX1fveXRa+9E6D59Db5yL+9lbLtv4tThS4YnXQJR4RWvgB4EWRUxhuHW79aVRflt+T5jBhlAkA1HdRL4Cg90dy30oqZJeTenvbWRTPsXzZ4GEEb498G+lEDTLZ3/Nc09n+KpQlE0U+HDDgqzT2K8PrOnJzKeOl5AkAHBjjOVnSn90Yr++BMvh33KSp/6wksGIIsr+WB5NhSe6RyPF1fpf0VFuPsvm6QA7zectO0KI6mjr4q0KuXHjwNAkEAgoCCh87+v1UX/UO520ZlQ8L2RL1jolmvvRSfsSDO/fLgKPGlqkXbUOE77IZJ/uImbW9r7EnbeZsW/f0SKVMnyA==";
    	PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(base64.decode(appSecret));
        KeyFactory keyf = KeyFactory.getInstance("RSA");
        PrivateKey priKey = keyf.generatePrivate(priPKCS8);

        java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHM);

        signature.initSign(priKey);
        signature.update(key.getBytes());

        byte[] signed = signature.sign();

        String sign = base64.encodeToString(signed);
        System.out.println(sign);
    }
}
